//
//  CPlayerControl.cpp
//  beast_tamer
//
//  Created by  bk on 13/1/20.
//
//

#include "CPlayerControl.h"
#include "InputDef.h"
#include "CTouchAction.h"
#include "CInputSystem.h"

using namespace MMA;

CPlayerControl::CPlayerControl()
{
    m_LockObject = NULL;
    
    m_TeamNymber = BATTLE_TEAM_RED;
}

CPlayerControl::~CPlayerControl()
{

}

void CPlayerControl::Init()
{
    if (CInputSystem::Exists() == false)
    {
        CInputSystem::Create();
    }
    
    CInputSystem::Get().AddListen(this);
}

void CPlayerControl::Update()
{
    
}

void CPlayerControl::HandleInput(INPUT_DATA* inputData)
{
    switch(inputData->m_inputID)
    {
        case TOUCH_INPUT_ID:
        {
            onProcessTouchInput(inputData);
            
            break;
        }
        case PET_1_INPUT_ID:
        {
            break;
        }
        default:
        {
            break;
        }
    }
}

void CPlayerControl::onProcessTouchInput(INPUT_DATA* inputData)
{
    TOUCH_DATA* tmp = (TOUCH_DATA*)inputData;
    
    if(m_LockObject == NULL && tmp->targetMember == NULL)
    {
        return;
    }
    else if(m_LockObject && tmp->targetMember == NULL)
    {
        //Move obj to Point
        CMoveCommand move;
        move.m_target = tmp->m_fTouchPoint;
        m_LockObject->processCommand(&move);
    }
    else if(m_LockObject == NULL && tmp->targetMember)
    {
        if (tmp->targetMember->GetTeamNumber() == m_TeamNymber)
        {
            m_LockObject = tmp->targetMember;
        }
    }
    else if(m_LockObject && tmp->targetMember)
    {
        if (m_LockObject->GetTeamNumber() == tmp->targetMember->GetTeamNumber())
        {
            // change Lock obj
            m_LockObject = tmp->targetMember;
        }
        else
        {
            // attack obj
            CAttackCommand attack;
            attack.targetObj = tmp->targetMember;
            m_LockObject->processCommand(&attack);
        }
    }
}




